#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#define FILENAME "log.txt"
int main()
{
    close(1); // close the stdout fd
    int fd = open(FILENAME,O_WRONLY|O_CREAT|O_APPEND,0666);
    if(fd<0)
    {
        perror("open file fail!");
        return 1;
    }
    const char* str = "hefjkdlsfjKFDJKSLF";
    //将标准输出重定向到文件
    write(fd,str,strlen(str));
    printf("fd = %d",fd); // 此时的输出就到了log.txt文件中了
    fprintf(stdout,"hello world");
    close(fd);
    return 0;
}
